package com.jiuwu.note;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jiuwu.note.dao.ShopEventDao;
import com.jiuwu.note.po.ShopDetailsPO;
import com.jiuwu.note.po.ShopEventPO;
import com.jiuwu.note.po.ShopersPO;
import com.jiuwu.note.service.ShopEventService;
import com.jiuwu.note.service.ShoperService;
import com.jiuwu.note.service.UserService;
import com.jiuwu.note.vo.InCome;


@Controller
public class NoteController {
	Logger logger = Logger.getLogger(NoteController.class);
	
	@Autowired
	UserService UserService;
	
	@Autowired
	ShopEventService shopEventService;
	
	@Autowired
	ShoperService shopService;
	
	
	@RequestMapping("/addUser")
	public ModelAndView addUser(@RequestParam(value="username", required=false) String userName){
		ModelAndView view = null;
		
		UserService.addUser(userName);
		
		
		logger.debug("debug userName "+(userName==null?"null":userName));
		logger.info("info userNam e"+(userName==null?"null":userName));
		logger.error("error userName "+(userName==null?"null":userName));
		
//		if(null == userName){
			view = new ModelAndView();
			view.setViewName("showadd");
//		}
		return view;
	}
	
	
	public ModelAndView addGoods(@RequestParam("goodsname") String goodsName,
			@RequestParam("goodsnameen")String goodsNameEnglish,@RequestParam("maningprice") Float maningPrice) {
		
		
		return null;
	}
	
	@RequestMapping("/addEvents")
	public ModelAndView addEvents(@RequestParam("title") String eventTitle,
			@RequestParam("rateReal")Float rateReal,@RequestParam("rateSell") Float rateSell) {
		ShopEventPO po = new ShopEventPO();
		po.setExchangeRateReal(rateReal);
		po.setExchangeRateSell(rateSell);
		po.setTitle(eventTitle);
		int rows = shopEventService.addEvent(po);
		if(rows > 0) {
			logger.debug("添加成功"+ po.toString());
		}else {
			logger.error("添加失败"+ po.toString());
		}
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/show");
		
		return view;
	}
	
	
	
	@RequestMapping("/show")
	public ModelAndView showAllEvent() {
		logger.info("info note showAllEvents");
		logger.debug("debug note showAllEvents");
		logger.error("error note showAllEvents");
		
		ModelAndView view = new ModelAndView();
		List<ShopEventPO> shops =  shopEventService.getAllEvent();
		view.addObject("shops", shops);
		view.setViewName("/showEvents");
		return view;
	}
	
	@RequestMapping("/events/showDetails")
	public ModelAndView showDetails(@RequestParam(value = "selectedId",required = false) Integer selectedId,
			@RequestParam("shopEventId") int shopEventId,
			HttpSession session) {
		ModelAndView view = new ModelAndView();
//		List<ShopEventPO> shops =  shopEventService.();
//		view.addObject("shops", shops);
		ShopEventPO po = shopEventService.getShopEventById(shopEventId);
		session.setAttribute("shopEvent", po);
		
		long start = System.currentTimeMillis();
		List<ShopersPO> shopers = shopService.getAllUser();
		List<ShopDetailsPO> listShopDetails = shopEventService.getShopDetailsByEventId(shopEventId);
		long end = System.currentTimeMillis();
		logger.debug("查询数据花时长："+(end - start));
		
		start = System.currentTimeMillis();
		InCome income = null;
		HashMap<String,List<ShopDetailsPO>> shopDetailsMap = new HashMap<>();
		
		float buy = 0  ,sale = 0;
		for(int i = 0 ; i < listShopDetails.size();i++) {
			ShopDetailsPO shopDetails = listShopDetails.get(i);
			if(!shopDetailsMap.containsKey(shopDetails.getShoperName())) {
				shopDetailsMap.put(shopDetails.getShoperName(), new ArrayList<ShopDetailsPO>());
			}
			shopDetailsMap.get(shopDetails.getShoperName()).add(shopDetails);
			buy += po.getExchangeRateReal() * shopDetails.getGoodsNum()* shopDetails.getGoodsPrice();
			sale += po.getExchangeRateSell() * shopDetails.getGoodsNum() * shopDetails.getGoodsPrice();
		}
		
		income = new InCome(po.getExchangeRateReal(), po.getExchangeRateSell(), buy, sale);
		
		end = System.currentTimeMillis();
		logger.debug("计算数据花费时长："+(end - start));
		
		view.addObject("details",shopDetailsMap);
		view.addObject("shopers", shopers);
		view.addObject("income",income);
		if(null == selectedId ) {
			if( shopers != null && (!shopers.isEmpty())) {
				selectedId = shopers.get(0).getId();
			}else {
				// 此时不存在数据
				selectedId = 0;
			}
			
		}
		view.addObject("selectedId",selectedId);
		
		view.setViewName("addEventDetail");
		return view;
	}
	
	@RequestMapping("/events/addDetail")
	public ModelAndView addDetails(@RequestParam("shoperId") int shoperId,
			@RequestParam("goodsName") String goodsName,
			@RequestParam("goodsNum") int goodsNum,
			@RequestParam("goodsPrice") float goodsPrice,
			HttpSession session) {
		
		ShopDetailsPO  po = new ShopDetailsPO();
		po.setShoperId(shoperId);
		ShopEventPO shopEevent = (ShopEventPO)session.getAttribute("shopEvent");
		po.setShopEventId(shopEevent.getId());
		po.setGoodsName(goodsName);
		po.setGoodsNum(goodsNum);
		po.setGoodsPrice(goodsPrice);
		po.setExchangeRate(shopEevent.getExchangeRateSell());
		
		int rows = shopEventService.addDetails(po);
		if(rows > 0) {
			logger.debug("添加成功"+ po.toString());
		}else {
			logger.error("添加失败"+ po.toString());
		}
		
		return new ModelAndView("redirect:/events/showDetails?selectedId="+shoperId+"&shopEventId="+shopEevent.getId());
	}
	
	
}
